using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace MKDS_Course_Editor
{
    public partial class Associations : Form
    {
        public Associations()
        {
            InitializeComponent();
        }

        private void Associations_Load(object sender, EventArgs e)
        {
            string[] ext = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").OpenSubKey("Extensions").GetValueNames();
            bool[] extok = new bool[ext.Length];
            for (int i = 0; i < ext.Length; i++)
            {
                extok[i] = Convert.ToBoolean((int)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").OpenSubKey("Extensions").GetValue(ext[i]));
                checkedListBox1.Items.Add(ext[i], extok[i]);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Associations_FormClosing(object sender, FormClosingEventArgs e)
        {
            RegistryKey k = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("MKDS Course Modifier").OpenSubKey("Extensions", true);
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                k.SetValue(checkedListBox1.Items[i].ToString(), Convert.ToInt32(checkedListBox1.GetItemChecked(i)));
				if (checkedListBox1.GetItemChecked(i))
				{
					if (!IsAssociated("." + checkedListBox1.Items[i].ToString()))
					{
						Associate("." + checkedListBox1.Items[i].ToString(), "MKDS Course Modifier." + checkedListBox1.Items[i].ToString(), Properties.Resources.ExtDesc.Split(',')[i], i + 1, Application.ExecutablePath);
					}
				}
            }
        }

		// Associate file extension with progID, description, icon and application
		public static void Associate(string extension,
			   string progID, string description, int icon, string application)
		{
			Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
			if (progID != null && progID.Length > 0)
				using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
				{
					if (description != null)
						key.SetValue("", description);
					if (icon != null)
						key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(application)+","+icon.ToString());
					if (application != null)
						key.CreateSubKey(@"Shell\Open\Command").SetValue("",
									ToShortPathName(application) + " \"%1\"");
				}
		}

		// Return true if extension already associated in registry
		public static bool IsAssociated(string extension)
		{
			return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
		}

		[DllImport("Kernel32.dll")]
		private static extern uint GetShortPathName(string lpszLongPath,
			[Out] StringBuilder lpszShortPath, uint cchBuffer);

		// Return short path format of a file name
		private static string ToShortPathName(string longName)
		{
			StringBuilder s = new StringBuilder(1000);
			uint iSize = (uint)s.Capacity;
			uint iRet = GetShortPathName(longName, s, iSize);
			return s.ToString();
		}

    }
}
